<?php
namespace app\admin\logic;
use think\Facade\Db;
use app\common\Apiv;
use app\common\Util;
/**
 * @Author: K.EL
 * @Date:   2020-07-20 00:04:13
 * @Last Modified by:   K.EL
 * @Last Modified time: 2020-08-18 17:04:28
 */
class MenuLogic{
	static function getTreeLists(){
		$role_id = input('role_id', 0);
		$status = input('status', '');
		$obj = Db::name('sys_menu')->field('id,parent_id,url,menu_name,is_show,sort,status');
		if($status){
			$obj->where('status', $status);
		}else{
			$obj->where('status', 'exp','>0');
		}
		$lists = $obj->order('sort asc,id asc')->select()->toArray();
		$isCheckedIds = [];
		if($role_id>0){
			$roleInfo = Db::name('sys_role')
							->field('id,role_name,role_menu_ids')
							->where('id', $role_id)
							->find();
			if($roleInfo && $roleInfo['role_menu_ids']){
				$isCheckedIds = explode(',', $roleInfo['role_menu_ids']);
			}
		}
		$lists = Util::init()->getMenuTree($lists,'parent_id',0,0,1,0,'children');
		return Apiv::result(1,0,'菜单获取', ['lists'=>$lists,'isCheckedIds'=>$isCheckedIds]);
	}

	//获取超级管理员的所有菜单
	static function getSupMenu(){
		$lists = Db::name('sys_menu')
					->field('id, parent_id,menu_name,url,icon,is_show,sort')
					->where('status',1)
					->order('sort asc, id asc')
					->cache(false)
					->select();

		if(!$lists){
			return Apiv::result(0,1301);
		}else{
			$topMenu = Util::init()->getTopMenu($lists);
			$leftMenu = Util::init()->getMenuTree($lists);
			return Apiv::result(1,0,'菜单获取成功',['topMenu'=>$topMenu['topMenu'],'leftMenu'=>$leftMenu,'moreMenu'=>$topMenu['moreMenu']]);
		}
	}

	//获取角色的菜单
	static function getRoleMenu($role_ids){
		if(!$role_ids){
			//没有角色，所以没有菜单
			// return Apiv::result(1,0,'菜单获取成功',['topMenu'=>[],'leftMenu'=>[],'moreMenu'=>[]]);
			return Apiv::result(0,1301);
		}
		$roleLists = Db::name('sys_role')
				->field('role_menu_ids')
				->where('id','in', $role_ids)
				->where('status',1)
				->select();
		if(!$roleLists){
			//角色没有菜单
			// return Apiv::result(1,0,'菜单获取成功',['topMenu'=>[],'leftMenu'=>[],'moreMenu'=>[]]);
			return Apiv::result(0,1301);
		}
		$menuIds = [];
		foreach($roleLists as $v){
			if(!$v['role_menu_ids']){
				continue;
			}else{
				$tmp = explode(',', $v['role_menu_ids']);
				$menuIds = array_merge($menuIds, $tmp);
			}
		}
		// var_dump($menuIds);
		$lists = Db::name('sys_menu')
					->field('id, parent_id,menu_name,url,icon,is_show,sort')
					->where('status',1)
					->where('id','in', $menuIds)
					->order('sort asc, id asc')
					->cache(false)
					->select();

		if(!$lists){
			return Apiv::result(0,1301);
		}else{
			$topMenu = Util::init()->getTopMenu($lists);
			$leftMenu = Util::init()->getMenuTree($lists);
			return Apiv::result(1,0,'菜单获取成功',['topMenu'=>$topMenu['topMenu'],'leftMenu'=>$leftMenu,'moreMenu'=>$topMenu['moreMenu']]);
		}
	}

	static function getInfo(){
		$id = input('id', 0);
		$info = Db::name('sys_menu')->where('id', $id)->find();
		return Apiv::result(1,0,'菜单成功',$info);
	}

	static function add(){
		$data = [];
		$data['menu_name'] = input('menu_name','');
		$data['url'] = input('url', '');
		$data['sort'] = input('sort', 50);
		$data['status'] = input('status', 1);
		$data['is_show'] = input('is_show', 1);
		$data['add_time'] = date('Y-m-d H:i:s');
		$data['icon'] = input('icon', '');
		$data['parent_id'] = input('parent_id', 0);
		$info = Db::name('sys_menu')->cache(true)->insert($data);
		return Apiv::result(1,0,'添加成功');
	}

	static function edit(){
		$data = [];
		$data['menu_name'] = input('menu_name','');
		$data['url'] = input('url', '');
		$data['sort'] = input('sort', 50);
		$data['status'] = input('status', 1);
		$data['is_show'] = input('is_show', 1);
		$data['icon'] = input('icon', '');
		$id = input('id', 0);
		$info = Db::name('sys_menu')->cache(true)->where('id', $id)->update($data);
		return Apiv::result(1,0,'编辑成功');
	}
}